
package com.yungu.swift.system.marketing.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.SystemConstant;
import com.yungu.swift.system.marketing.dao.MarketingGiftCodeMapper;
import com.yungu.swift.system.marketing.model.dto.MarketingGiftCodeDto;
import com.yungu.swift.system.marketing.service.MarketingGiftCodeService;
import com.yungu.swift.system.marketing.thread.MarketingGiftCodeThread;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static com.yungu.swift.system.marketing.model.enums.MarketingErrorEnum.CDKEY_INVALID;
import static com.yungu.swift.system.marketing.model.enums.MarketingErrorEnum.CDKEY_USED;
import static com.yungu.swift.system.marketing.model.enums.MarketingErrorEnum.CDKEY_REPEAT;

/**
 * <p>
 * 优惠券礼包业务逻辑处理接口实现类
 *
 * @author xiuyin.cui - Generate
 * @version 1.1.0-SNAPSHOT
 * @since 2019-04-25 13:50
 */
@Service
public class MarketingGiftCodeServiceImpl extends BaseServiceImpl<MarketingGiftCodeDto> implements MarketingGiftCodeService {

    /**
     * 兑换码/口令码类型 1 兑换码 2 口令码
     */
    public final static int TYPE_MARKETING_GIFT_CDKEY = 1;
    public final static int TYPE_MARKETING_GIFT_COMMAND = 2;

    @Autowired
    private MarketingGiftCodeMapper marketingGiftCodeMapper;

    @Override
    protected IMapper<MarketingGiftCodeDto> getMapper() {
        return marketingGiftCodeMapper;
    }

    @Override
    public ResponseData<Boolean> verifyCode(String code) {
        MarketingGiftCodeDto gift = marketingGiftCodeMapper.findByCode(code);
        if (null == gift) {
            return ResponseData.buildErrorResponse(CDKEY_INVALID.getCode(), CDKEY_INVALID.getMsg(), Boolean.FALSE);
        }
        if (null != gift.getCodeType() && Integer.valueOf(gift.getCodeType()) == TYPE_MARKETING_GIFT_CDKEY) {
            if(gift.getUsed() == SystemConstant.VERIFY_MARKETING_GIFT_USED){
                return ResponseData.buildErrorResponse(CDKEY_USED.getCode(), CDKEY_USED.getMsg(), Boolean.FALSE);
            }
        }
        return ResponseData.buildSuccessResponse(Boolean.TRUE);
    }

    @Override
    public ResponseData<Boolean> disableCode(String code) {
        return ResponseData.buildSuccessResponse(marketingGiftCodeMapper.disableCode(code) > 0);
    }

    @Override
    public ResponseData<List<String>> findRepeatRedeemCodes(List<String> redeemCodeList) {
        return ResponseData.buildSuccessResponse(marketingGiftCodeMapper.findRepeatRedeemCodes(redeemCodeList));
    }

    @Override
    public ResponseData<List<String>> listCDKeyByGiftUuid(String giftUuid) {
        return ResponseData.buildSuccessResponse(marketingGiftCodeMapper.listCDKeyByGiftUuid(giftUuid));
    }

    /**
     * 校验礼包口令是否存在
     * @param code 参数
     * @return
     */
    @Override
    public ResponseData<Boolean> findCodeExists(String code) {
        MarketingGiftCodeDto gift = marketingGiftCodeMapper.findByCode(code);
        if (null == gift) {
            return ResponseData.buildSuccessResponse(Boolean.TRUE);
        }
        return ResponseData.buildErrorResponse(CDKEY_REPEAT.getCode(), CDKEY_REPEAT.getMsg(), Boolean.FALSE);
    }
}
